package cn.demomaster.quickui_sample.ui.fragment.helper;

import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.demomaster.quickcompat.widget.button.QuickButton;
import com.demomaster.quickcompat.widget.button.QuickToggleButton;

import cn.demomaster.quickrouter.fragment.QuickFragment;
import cn.demomaster.quickui.annotation.ActivityPager;
import cn.demomaster.quickui.annotation.ResType;
import cn.demomaster.quickui.helper.BatteryOptimizationsHelper;
import cn.demomaster.quickui.helper.QuickNotification;
import cn.demomaster.quickui_sample.R;

/**
 * Squirrel桓
 * 2018/8/25
 */

@ActivityPager(name = "消息通知", preViewClass = TextView.class, resType = ResType.Resource)//,iconRes = R.drawable.ic_notify)
public class NotifycationFragment extends QuickFragment {


    TextView tv_state;
    QuickToggleButton tb_state;
    QuickButton btn_01;
    QuickButton btn_02;
    QuickButton btn_03;
    QuickButton btn_04;
    QuickButton btn_05;
    QuickButton btn_06;

    @NonNull
    @Override
    public View onGenerateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View mView = inflater.inflate(R.layout.fragment_layout_notifycation, null);
        return mView;
    }

    public void initView(View rootView) {
        tv_state = findViewById(R.id.tv_state);
        tb_state = findViewById(R.id.tb_state);
        btn_01 = findViewById(R.id.btn_01);
        btn_02 = findViewById(R.id.btn_02);
        btn_03 = findViewById(R.id.btn_03);
        btn_04 = findViewById(R.id.btn_04);
        btn_05 = findViewById(R.id.btn_05);
        btn_06 = findViewById(R.id.btn_06);
        getActionBarTool().setTitle("消息通知");

        tb_state.setDefChecked(QuickNotification.isNotificationEnabled(mContext));
        tb_state.setOnToggleChanged(new QuickToggleButton.OnToggleChangeListener() {
            @Override
            public void onToggle(View view, boolean on) {
                QuickNotification.startNotificationSetting(mContext);
            }
        });
        btn_01.setOnClickListener(v -> BatteryOptimizationsHelper.request(getContext()));
        btn_02.setOnClickListener(v -> {
            QuickNotification.Builer builer = new QuickNotification.Builer(mContext);
            builer.setTitle("消息通知（系统默认）").setContentText(""+((TextView)v).getText()).send();
        });
        btn_03.setOnClickListener(v -> {
            QuickNotification.Builer builer = new QuickNotification.Builer(mContext);
            String soundResourceName = "beep";
            String soundResourceName2 = "audio/beep.ogg";
            String path = "file:///android_asset/"+ soundResourceName;
            Uri soundUri = null;
            //访问Resource
            //soundUri = NotificationHelper2.getUriFromResource(mContext,soundResourceName);
            //访问Assets
            soundUri = QuickNotification.getUriFromAssets(mContext,soundResourceName2);
            builer.setTitle("消息通知（自定义音频）").setContentText(""+((TextView)v).getText()).setSoundUri(soundUri).send();
        });
        btn_04.setOnClickListener(v -> {
            QuickNotification.Builer builer = new QuickNotification.Builer(mContext);
            builer.setTitle("消息通知（静音+震动）").setContentText(""+((TextView)v).getText()).setEnableSound(false).send();
        });

        btn_05.setOnClickListener(v -> {
            QuickNotification.Builer builer = new QuickNotification.Builer(mContext);
            builer.setTitle("消息通知（静音不震动）").setContentText(""+((TextView)v).getText()).setEnableVibration(false).setEnableSound(false).send();
        });

        btn_06.setOnClickListener(v -> {
            QuickNotification.Builer builer = new QuickNotification.Builer(mContext);
            builer.setTitle("消息通知（自定义视图）")
                    .setEnableVibration(false)
                    .setEnableSound(false)
                    .setContentText("")
                    .send();
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        tb_state.setDefChecked(QuickNotification.isNotificationEnabled(mContext));
    }
}